package com.ccctop.crowd.handler;

import com.ccctop.crowd.api.MySQLRemoteService;
import com.ccctop.crowd.config.Web3jConfig;
import com.ccctop.crowd.constant.CrowdConstant;
import com.ccctop.crowd.entity.po.TWallet;
import com.ccctop.crowd.entity.vo.MemberLoginVO;
import com.ccctop.crowd.util.ResultEntity;
import com.ccctop.crowd.util.Web3jUtil;
import com.ccctop.crowd.vo.HaveKeyStoreVo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.HashMap;

@RestController
@RequestMapping("/wallet")
public class WalletHandler {
    @Autowired
    MySQLRemoteService mySQLRemoteService;
    @Autowired
    ObjectMapper objectMapper;

    @RequestMapping("/hasWallet")
    public ResultEntity<HaveKeyStoreVo> hasWallet(HttpSession httpSession) {
        try {
            MemberLoginVO user = (MemberLoginVO) httpSession.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER);
            ResultEntity<TWallet> result = mySQLRemoteService.selectWalletByUserId(user.getId());
            if (result.getResult().equals(ResultEntity.SUCCESS)) {
                HaveKeyStoreVo haveKeyStoreVo = new HaveKeyStoreVo();
                if (result.getData() != null) {
                    haveKeyStoreVo.setHaveKeyStore(true);
                } else {
                    haveKeyStoreVo.setHaveKeyStore(false);
                }
                return ResultEntity.successWithData(haveKeyStoreVo);
            }else {
                return ResultEntity.failed(result.getMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }
    }

    @RequestMapping("/upload")
    public ResultEntity<String> walletUpload(@RequestParam("wallet") MultipartFile file,HttpSession httpSession){
        try {
            String originalFilename = file.getOriginalFilename();
            //数据库保存
            MemberLoginVO user = (MemberLoginVO) httpSession.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER);
            TWallet wallet = new TWallet();
            wallet.setUserId(user.getId());
            wallet.setWallet(originalFilename);
            ResultEntity<String> result = mySQLRemoteService.insertWallet(wallet);
            if (result.getResult().equals(ResultEntity.FAILED)){
                return result;
            }
            //本地保存
            file.transferTo(new File(Web3jConfig.WALLET_PATH_PREFIX+originalFilename));
            return ResultEntity.successWithoutData();
        }catch (Exception e){
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }
    }
}
